(function () {
  'use strict';

  angular
    .module('vboxApp')
    .controller('translateCtrl', translateCtrl)
    .controller('authCtrl', authCtrl)
    .controller('callCtrl', callCtrl)
    .controller('appCtrl', appCtrl)
    .controller('sidebarController', sidebarController);


  /** @ngInject */
  function translateCtrl($translate, $scope) {
    $scope.changeLanguage = function (langKey) {
      $translate.use(langKey);
    };
  }

  /** @ngInject */
  function authCtrl($scope, authService, $location) {
    $scope.logout = function () {
      $location.path('/login');
      authService.logout();
    };
  }

  /** @ngInject */
  function callCtrl($scope, $http) {
    $scope.call = function () {
      $http.get($scope.url).success(function (data) {
        $scope.json = JSON.stringify(data, null, 4);
      }).error(function (e) {
        $scope.json = JSON.stringify(e, null, 4);
      });
    };
  }

  /** @ngInject */
  function appCtrl($scope, $rootScope, $state) {

    $scope.$state = $state;

    $scope.$on('$includeContentLoaded', function () {
      App.initSlimScroll();
    });

    $scope.$on('$stateChangeStart', function () {
      $rootScope.setting.layout.pageSidebarMinified = false;
      $rootScope.setting.layout.pageFixedFooter = false;
      $rootScope.setting.layout.pageRightSidebar = false;
      $rootScope.setting.layout.pageTwoSidebar = false;
      $rootScope.setting.layout.pageTopMenu = false;
      $rootScope.setting.layout.pageBoxedLayout = false;
      $rootScope.setting.layout.pageWithoutSidebar = false;
      $rootScope.setting.layout.pageContentFullHeight = false;
      $rootScope.setting.layout.pageContentFullWidth = false;
      $rootScope.setting.layout.paceTop = false;
      $rootScope.setting.layout.pageLanguageBar = false;
      $rootScope.setting.layout.pageSidebarTransparent = false;
      $rootScope.setting.layout.pageWideSidebar = false;
      $rootScope.setting.layout.pageLightSidebar = false;
      $rootScope.setting.layout.pageFooter = false;
      $rootScope.setting.layout.pageMegaMenu = false;
      $rootScope.setting.layout.pageWithoutHeader = false;
      $rootScope.setting.layout.pageBgWhite = false;
      $rootScope.setting.layout.pageContentInverseMode = false;

      App.scrollTop();

      $('.pace .pace-progress').addClass('hide');
      $('.pace').removeClass('pace-inactive');
    });
    $scope.$on('$stateChangeSuccess', function () {
      Pace.restart();
      App.initPageLoad();
      setTimeout(function () {
        App.initLocalStorage();
        App.initComponent();
      }, 0);
    });
    $scope.$on('$stateNotFound', function () {
      Pace.stop();
    });
    $scope.$on('$stateChangeError', function () {
      Pace.stop();
    });

  }


  /* -------------------------------
   2.0 CONTROLLER - Sidebar
   ------------------------------- */

  /** @ngInject */
  function sidebarController($scope, $state) {
    $scope.$state = $state;
    App.initSidebar();
    App.initSidebarSelection();
  }

  /* -------------------------------
   4.0 CONTROLLER - Header
   ------------------------------- */
  angular
    .module('vboxApp').controller('headerController', function ($uibModal, $scope,dialogs,authService) {
    App.initRightSidebarToggle();

    $scope.resetPassword = function () {
      var modal = $uibModal.open({
        animation: false,
        size: 'md',
        backdrop: 'static',
        templateUrl: 'app/components/layout/resetPasswordModal.html',
        controller: 'resetPasswordCtrl',
        resolve: {
          vm: function () {
            return {};
          }
        }
      });
      modal.result.then(function (resetInfo) {
        authService.resetPassword(resetInfo, function (data) {
          console.info(data);
          if (data.success) {
            dialogs.notify("通知", "密码修改成功！", {size: 'sm', windowClass: 'dialogs-inverse'});
          }
        });
      });
    };
  });

  /* -------------------------------
   6.0 CONTROLLER - Page Loader
   ------------------------------- */
  angular
    .module('vboxApp').controller('pageLoaderController', function () {
    App.initPageLoad();
    App.initRightSidebarToggle();


  });


  /* -------------------------------
   7.0 CONTROLLER - Theme Panel
   ------------------------------- */
  angular
    .module('vboxApp').controller('themePanelController', function () {
    App.initThemePanel();
  });

  /* -------------------------------
   8.0 CONTROLLER - Message Panel
   ------------------------------- */
  angular
    .module('vboxApp').controller('messagePanelController', function () {
    App.initMessagePanel();
  });


  /* -------------------------------
   9.0 CONTROLLER - Reset Password
   ------------------------------- */
  angular
    .module('vboxApp').controller('resetPasswordCtrl', function ($uibModalInstance, $scope,$rootScope) {

    $scope.save = function () {
      var resetInfo = {
        "userName": $rootScope.currentUser.user_name,
        "originPassword": $scope.originPassword,
        "newPassword": $scope.newPassword
      };
      $uibModalInstance.close(resetInfo);
    };

    $scope.close = function () {
      $uibModalInstance.dismiss('cancel');
    };
  });
})();
